<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class MeToCurrentUserId
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $parameters = $request->route()->parameters();

        foreach ($parameters as $key => $parameter) {
            if (strtolower($parameter) === 'me') {
                $request->route()->setParameter($key, Auth::id());
            }
        }

        return $next($request);
    }
}
